package com.cjjd.c.feature.hkb;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.ActionBarFragment;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.ToExchangeHkGoldBean;
import com.cjjd.c.feature.dialog.EditTextDialog;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.utils.SharePrefUtil;
import com.cjjd.c.utils.ToastUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by dada on 2017/12/23.
 */

public class ExchangeHkGoldFragment extends ActionBarFragment {
    @Bind(R.id.hknum)
    TextView hknum;
    @Bind(R.id.bili)
    TextView bili;
    @Bind(R.id.tv1)
    TextView tv1;
    @Bind(R.id.num)
    TextView num;
    @Bind(R.id.all)
    TextView all;

    private String cardId;
    private  ToExchangeHkGoldBean bean;
    @Override
    public Object getTitleString() {
        return "兑换好客币";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_exchange_hkb;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cardId = getArguments().getString("id");
        tonet();
    }

    private void tonet() {


        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("cardUuid", cardId);

        Call call = service.toExchangeHkGold(JsonUtils.convertParam("toExchangeHkGold", h1));
        call.enqueue(new CallbackImpl<ToExchangeHkGoldBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<ToExchangeHkGoldBean>> call, Response<HttpResult<ToExchangeHkGoldBean>> response) {
                bean = response.body().data;
                hknum.setText(bean.hkGoldNum);
                bili.setText("本酒店积分兑换比例 1积分=" + bean.proportion + "好客币，当前积分：" + bean.integral);
            }
        });

    }


    private void send() {

        String nums=num.getText().toString();
        if(TextUtils.isEmpty(nums)){
            ToastUtil.showToast("请输入好客币");
            return;
        }
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));

        h1.put("integral", nums);
        h1.put("cardUuid",cardId);

        Call call = service.exchangeHkGold(JsonUtils.convertParam("exchangeHkGold", h1));
        call.enqueue(new CallbackImpl<Object>() {
            @Override
            public void responseSuccess(Call<HttpResult<Object>> call, Response<HttpResult<Object>> response) {
                ToastUtil.showToast("兑换成功");
                mContext.finish();
            }
        });

    }

    @OnClick({R.id.num, R.id.all, R.id.send})
    public void onViewClicked(View view) {
        if(bean==null){
            tonet();
            ToastUtil.showToast("数据异常");
            return;
        }
        switch (view.getId()) {
            case R.id.num:
                new EditTextDialog(mContext, "请输入兑换的积分数", new EditTextDialog.OnCloseListener() {
                    @Override
                    public void OnClick(String message) {
                        num.setText(message);
                    }
                }).setJP().show();
                break;
            case R.id.all:
                num.setText(bean.integral);
                break;
            case R.id.send:

                send();

                break;
        }
    }
}
